package com.feishi.core.analytic.packing.contentpacking;

import com.feishi.core.util.common.ByteUtil;
import com.feishi.core.util.common.bean.ByteBufferProxy;
import com.feishi.core.analytic.body.AnalyticBody;
import com.feishi.core.analytic.element.AnalyticContentBodyElement;
import com.feishi.core.analytic.element.AnalyticContentElement;
import com.feishi.core.analytic.packing.AnalyticDataPacking;
import com.feishi.core.analytic.packing.debuild.DeBuildSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by caixq on 5/16 016.
 */
public class AnalyticContentBodyPacking  extends AnalyticContentPacking implements ContentNumberPacking{
    private AnalyticContentBodyElement contentElement;

    private byte[] number;
    private List<AnalyticDataPacking> contentPackings=new ArrayList<>();


    public AnalyticContentBodyPacking(AnalyticContentBodyElement contentElement) {
        this.contentElement=contentElement;
    }


    public byte[] getNumber() {
        return number;
    }

    public void setNumber(byte[] number) {
        this.number = number;
    }

    public void initData() {
        if(this.content!=null){
            return;
        }
        ByteBufferProxy bfp=new ByteBufferProxy();
        if(this.number==null){
            this.number=contentElement.getNumberElement().getBytes(contentPackings.size());
        }
        for (AnalyticDataPacking packing : contentPackings) {
            packing.addSession(buildSession);
            bfp.put(packing.build());
        }
        this.content=bfp.get();
    }

    @Override
    public AnalyticContentElement getContentElement() {
        return contentElement;
    }


    public AnalyticDataPacking getNewContentDataPacking() {

        AnalyticBody body =contentElement.getBody();
        AnalyticDataPacking contentDataPacking = body.analyticBuildPacking().analyticDataPacking();
        contentPackings.add(contentDataPacking);
        return contentDataPacking;
    }

    public List<AnalyticDataPacking> getContentDataPacking() {
        return contentPackings;
    }

    public int getContentLength() {
        int number= (int) contentElement.getNumberElement().getValue(this.number);
        return number*getContentItemLength();
    }

    @Override
    public void debuildContent(byte[] bytes) {
        int num= (int) this.contentElement.getNumberElement().getValue(this.number);
        contentPackings.clear();
        for (int i = 0; i < num; i++) {
            AnalyticDataPacking contentDataPacking = getNewContentDataPacking();
            byte[] bs=ByteUtil.get(bytes,i*contentDataPacking.getStaticLength(),contentDataPacking.getStaticLength());
            contentDataPacking.buildSession=contentDataPacking.deBuildSession= (DeBuildSession) buildSession;
            contentDataPacking.deBuild(bs);
        }
        ByteBufferProxy bfp=new ByteBufferProxy();
        for (AnalyticDataPacking packing : contentPackings) {
            bfp.put(packing.build());
        }
        this.content=bfp.get();
    }

    public int getContentItemLength() {
        return (this.contentPackings.size()==0?(contentElement.getBody().analyticBuildPacking().analyticDataPacking()):this.contentPackings.get(0)).getStaticLength();
    }


    public void debuildContent() {
        if(this.contentPackings.size()==0){
            this.getNewContentDataPacking();
        }
        int length=getContentItemLength();
        AnalyticDataPacking contentDataPacking = null;
        int index=0;
        int num=content.length/length;
        if(num==0){
            this.contentPackings.clear();
        }
        while (index<num){
            if(index>=this.contentPackings.size()){
                getNewContentDataPacking();
                continue;
            }
                contentDataPacking=this.contentPackings.get(index);
            byte[] bytes=ByteUtil.get(content,index*length,length);
            contentDataPacking.deBuild(bytes);
            index++;
        }
    }
}
